<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- 课程详情 开始 -->
    <section class="container">

      <!-- 课程所属分类 开始 -->
      <section class="path-wrap txtOf hLh30">
        <a href="/" title class="c-999 fsize14">首页</a>
        \
        <a href="/course" title class="c-999 fsize14">课程列表</a>
        \
        <span :href="'/course/'+course.subjectLevelOneId" class="c-333 fsize14">{{ course.subjectLevelOne }}</span>
        \
        <span :href="'/course/'+course.subjectLevelTwoId" class="c-333 fsize14">{{ course.subjectLevelTwo }}</span>
      </section>
      <!-- /课程所属分类 结束 -->

      <!-- 课程基本信息 开始 -->
      <div>
        <article class="c-v-pic-wrap" style="height: 357px;">
          <section id="videoPlay" class="p-h-video-box">
            <img :src="course.cover" :alt="course.title" class="dis c-v-pic">
          </section>
        </article>
        <aside class="c-attr-wrap">
          <section class="ml20 mr15">
            <h2 class="hLh30 txtOf mt15">
              <span class="c-fff fsize24">{{ course.title }}</span>
            </h2>
            <section class="c-attr-jg">
              <span class="c-fff">价格：</span>
              <b class="c-yellow" style="font-size:24px;">{{ course.price===0?'免费':'￥'+course.price }}</b>
            </section>
            <section class="c-attr-mt c-attr-undis">
              <span class="c-fff fsize14">主讲：{{ course.teacherName }}&nbsp;&nbsp;&nbsp;</span>
            </section>
            <section class="c-attr-mt of">
              <span v-if="!isCollect" class="ml10 vam">
                <em class="icon18 scIcon"/>
                <a class="c-fff vam" title="收藏" href="javascript:void(0);" @click="addCollect()">收藏</a>
              </span>
              <span v-if="isCollect" class="ml10 vam">
                <em class="icon18 scIcon"/>
                <a class="c-fff vam" title="取消收藏" href="javascript:void(0);" @click="removeCollect()">已收藏</a>
              </span>
            </section>
            <section v-if="!isBuy && course.price!==0" class="c-attr-mt">
              <a
                href="javascript:void(0);"
                title="立即购买"
                class="comm-btn c-btn-2"
                @click="createOrder()">立即购买</a>
            </section>
            <section v-if="isBuy || course.price===0" class="c-attr-mt">
              <a href="#" title="立即观看" class="comm-btn c-btn-2">立即观看</a>
            </section>
          </section>
        </aside>
        <aside class="thr-attr-box">
          <ol class="thr-attr-ol">
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">购买数</span>
                <br>
                <h6 class="c-fff f-fM mt10">{{ course.buyCount }}</h6>
              </aside>
            </li>
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">课时数</span>
                <br>
                <h6 class="c-fff f-fM mt10">>{{ course.lessonNum }}</h6>
              </aside>
            </li>
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">浏览数</span>
                <br>
                <h6 class="c-fff f-fM mt10">{{ course.viewCount }}</h6>
              </aside>
            </li>
          </ol>
        </aside>
        <div class="clear"/>
      </div>
      <!-- /课程基本信息 结束 -->

      <div class="mt20 c-infor-box">
        <article class="fl col-7">
          <section class="mr30">
            <div class="i-box">
              <div>
                <section id="c-i-tabTitle" class="c-infor-tabTitle c-tab-title">
                  <a name="c-i" class="current" title="课程详情">课程详情</a>
                </section>
              </div>
              <article class="ml10 mr10 pt20">

                <!-- 课程详情介绍 开始 -->
                <div>
                  <h6 class="c-i-content c-infor-title">
                    <span>课程介绍</span>
                  </h6>
                  <div class="course-txt-body-wrap">
                    <section class="course-txt-body" v-html="course.description ">
                      <!-- 将内容中的html翻译过来  {{ course.description }} -->

                    </section>
                  </div>
                </div>
                <!-- /课程详情介绍 结束 -->

                <!-- 课程大纲 开始-->
                <div class="mt50">
                  <h6 class="c-g-content c-infor-title">
                    <span>课程大纲</span>
                  </h6>
                  <section class="mt20">
                    <div class="lh-menu-wrap">
                      <menu id="lh-menu" class="lh-menu mt10 mr10">
                        <ul>
                          <!-- 课程章节目录 -->
                          <li v-for=" chapter in chapterList " :key="chapter.id" class="lh-menu-stair">
                            <a :title="chapter.title" href="javascript: void(0)" class="current-1">
                              <em class="lh-menu-i-1 icon18 mr10"/>{{ chapter.title }}
                            </a>
                            <ol class="lh-menu-ol" style="display: block;">
                              <li v-for=" video in chapter.children " :key="video.id" class="lh-menu-second ml30">
                                <a :href="'/player/'+video.videoSourceId" title>
                                  <span class="fr">
                                    <i class="free-icon vam mr10">{{ video.free?'免费试听':'' }}</i>
                                  </span>
                                  <em class="lh-menu-i-2 icon16 mr5">&nbsp;</em>{{ video.title }}
                                </a>
                              </li>
                            </ol>
                          </li>
                        </ul>
                      </menu>
                    </div>
                  </section>
                  <!-- /课程大纲 结束 -->
                </div>
              </article>
            </div>
          </section>
        </article>
        <aside class="fl col-3">
          <div class="i-box">
            <!-- 主讲讲师 开始-->
            <div>
              <section class="c-infor-tabTitle c-tab-title">
                <a title href="javascript:void(0)">主讲讲师</a>
              </section>
              <section class="stud-act-list">
                <ul style="height: auto;">
                  <li>
                    <div class="u-face">
                      <a :href="'/teacher/'+ course.teacherId " target="_blank">
                        <img :src="course.avatar" width="50" height="50" alt>
                      </a>
                    </div>
                    <section class="hLh30 txtOf">
                      <a :href="'/teacher/'+ course.teacherId " class="c-333 fsize16 fl" target="_blank">{{
                      course.teacherName }}</a>
                    </section>
                    <section class="hLh20 txtOf">
                      <span class="c-999">尚硅谷{{ course.intro }}</span>
                    </section>
                  </li>
                </ul>
              </section>
            </div>
            <!-- /主讲讲师 结束 -->
          </div>
        </aside>
        <div class="clear"/>
        <!-- 添加评论，评论列表-->
        <aside>
          <section class="comment">
            <el-form>
              <el-form-item label="评论">
                <el-input v-model="comment.content" type="textarea"/>
              </el-form-item>
              <el-form-item>
                <el-button :disabled="isAble" @click="addComment">回复</el-button>
              </el-form-item>
            </el-form>
          </section>
          <section class="c-infor-tabTitle c-tab-title">
            <a name="c-i" class="current" title="评论列表">评论列表</a>
          </section>
          <div class="infinite-list-wrapper" style="overflow:auto">
            <ul
              v-infinite-scroll="load"
              class="list"
              infinite-scroll-distance="300px"
              infinite-scroll-disabled="disabled">
              <li v-for="(i, index) in comments" :key="index" class="list-item">
                <el-row>
                  <el-col :span="4">
                    <img :src="i.avatar" width="50" height="50" alt>
                    {{ i.nickname }}
                  </el-col>
                  <el-col :span="8">
                    {{ i.content }}
                  </el-col>
                </el-row>
              </li>
            </ul>
            <p v-if="loading">加载中...</p>
            <p v-if="noMore">没有更多了</p>
          </div>
          <!--<section class="infinite-list-wrapper" style="overflow:auto">-->
          <!--<ul v-infinite-scroll="load" class="infinite-list" style="overflow:auto">-->
          <!--<li v-for="(i, index) in comments" :key="index" class="infinite-list-item">-->
          <!--<el-row >-->
          <!--<el-col :span="4" >-->
          <!--<image-->
          <!--:src="i.avatar"-->
          <!--/>-->
          <!--<span>{{ i.nickName }}</span>-->
          <!--</el-col>-->
          <!--<el-col :span="8" >-->
          <!--<span>{{ i.content }}</span>-->
          <!--</el-col>-->
          <!--</el-row>-->
          <!--</li>-->
          <!--<p v-if="loading">加载中...</p>-->
          <!--<p v-if="noMore">没有更多了</p>-->
          <!--</ul>-->
          <!--</section>-->
        </aside>
      </div>

    </section>
    <!-- /课程详情 结束 -->
  </div>
</template>
<script>
import courseApi from '~/api/course'
import orderApi from '~/api/order'
import cookie from 'js-cookie'
import collectApi from '~/api/collect'
import commentApi from '~/api/comment'

export default {
  // 服务器端渲染方案：
  // 异步数据获取：在前端服务器端执  行
  async asyncData(page) {
    const response = await courseApi.getById(page.route.params.id)
    return {
      course: response.data.course,
      chapterList: response.data.chapterVoList
    }
  },
  data() {
    return {
      isBuy: false,
      isCollect: false,
      isAble: false,
      comment: {
        courseId: ''
      },
      cover: 'cover',
      comments: [],
      loading: false,
      currentPage: 1,
      size: 10,
      total: 10
    }
  },
  computed: {
    noMore() {
      return this.total < this.currentPage * this.size
    },
    disabled() {
      return this.loading || this.noMore
    }
  },
  created() {
    this.checkBuy()
    this.checkCollect()
  },
  methods: {
    errorHandler() {
      return true
    },
    createOrder() {
      console.log(111)
      orderApi.saveOrder(this.course.id).then(response => {
        this.$router.push({ path: '/order/' + response.data.orderId })
      })
    },
    checkBuy() {
      if (cookie.get('cjc_edu_token')) {
        orderApi.isBuy(this.course.id).then(res => {
          this.isBuy = res.data.isBuy
        })
      }
    },
    checkCollect() {
      if (cookie.get('cjc_edu_token')) {
        collectApi.isCollect(this.course.id).then(res => {
          this.isCollect = res.data.isCollect
        })
      }
    },
    addCollect() {
      if (cookie.get('cjc_edu_token')) {
        collectApi.addCollect(this.course.id).then(res => {
          this.$message({
            message: res.message
          })
          window.location.reload()
        })
      }
    },
    removeCollect() {
      if (cookie.get('cjc_edu_token')) {
        this.$confirm('确认要取消吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return collectApi.removeById(this.course.id)
        }).then((response) => {
          this.$message({
            type: 'success',
            message: response.message
          })
          window.location.reload()
        }).catch(error => {
          if (error === 'cancel') {
            this.$message({
              message: '已取消'
            })
          }
        })
      }
    },
    addComment() {
      this.comment.courseId = this.course.id
      this.comment.teacherId = this.course.teacherId
      this.isAble = true
      commentApi.addComment(this.comment).then(res => {
        this.$message.success(res.message)
        this.isAble = false
      }).catch(err => {
        console.log(err)
        this.isAble = false
      })
    },
    load() {
      this.loading = true
      setTimeout(() => {
        commentApi.listComment(this.currentPage, this.size, this.course.id).then(res => {
          this.comments.push(...res.data.comments)
          console.log(this.comments)
          this.currentPage++
          this.total = res.data.total
        })
        this.loading = false
      }, 2000)
    }
  }
}
</script>
<style scoped>
  a .comm-btn .c-btn-2 {
    width: 120px;
  }
  p {
   text-align: center;
  }
</style>
